package swing.hibernate.JFrame;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import dao.ConvertImage;
import dao.HashPassword;
import dao.TaiKhoanDAO;
import entities.TaiKhoan;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JInternalFrameNewAccount extends JInternalFrame {
	private JTextField textFieldUserName;
	private JPasswordField passwordField;
	private JTextField textFieldHoTen;
	private JTextField textFieldEmail;
	private JTextField textFieldDienThoai;
	private JTextField textFieldCongTy;
	private JTextField textFieldDiaChi;
	private JTextField textFieldAvatar;
	private TaiKhoan tk;

	/**
	 * Create the frame.
	 */
	public JInternalFrameNewAccount() {
		setClosable(true);
		setBounds(100, 100, 420, 335);
		setTitle("Add New Account");
		getContentPane().setLayout(null);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Information Account",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 377, 272);
		getContentPane().add(panel);
		panel.setLayout(null);

		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(24, 25, 86, 14);
		panel.add(lblUsername);

		textFieldUserName = new JTextField();
		textFieldUserName.setBounds(121, 22, 229, 20);
		panel.add(textFieldUserName);
		textFieldUserName.setColumns(10);

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(24, 50, 86, 14);
		panel.add(lblPassword);

		passwordField = new JPasswordField();
		passwordField.setBounds(121, 47, 229, 20);
		panel.add(passwordField);

		JLabel lblHTn = new JLabel("Họ Tên");
		lblHTn.setBounds(24, 78, 86, 14);
		panel.add(lblHTn);

		textFieldHoTen = new JTextField();
		textFieldHoTen.setBounds(121, 75, 229, 20);
		panel.add(textFieldHoTen);
		textFieldHoTen.setColumns(10);

		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(24, 103, 86, 14);
		panel.add(lblEmail);

		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(121, 100, 229, 20);
		panel.add(textFieldEmail);
		textFieldEmail.setColumns(10);

		JLabel lblinThoi = new JLabel("Điện Thoại");
		lblinThoi.setBounds(24, 128, 86, 14);
		panel.add(lblinThoi);

		textFieldDienThoai = new JTextField();
		textFieldDienThoai.setBounds(121, 125, 229, 20);
		panel.add(textFieldDienThoai);
		textFieldDienThoai.setColumns(10);

		JLabel lblCngTy = new JLabel("Công Ty");
		lblCngTy.setBounds(24, 153, 86, 14);
		panel.add(lblCngTy);

		textFieldCongTy = new JTextField();
		textFieldCongTy.setBounds(121, 150, 229, 20);
		panel.add(textFieldCongTy);
		textFieldCongTy.setColumns(10);

		JLabel lblaCh = new JLabel("Địa Chỉ");
		lblaCh.setBounds(23, 178, 87, 14);
		panel.add(lblaCh);

		textFieldDiaChi = new JTextField();
		textFieldDiaChi.setBounds(121, 175, 229, 20);
		panel.add(textFieldDiaChi);
		textFieldDiaChi.setColumns(10);

		JLabel lblAvatar = new JLabel("Avatar");
		lblAvatar.setBounds(24, 201, 46, 14);
		panel.add(lblAvatar);

		textFieldAvatar = new JTextField();
		textFieldAvatar.setBounds(121, 198, 159, 20);
		panel.add(textFieldAvatar);
		textFieldAvatar.setColumns(10);

		JButton btnM = new JButton("Mở");
		btnM.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setDialogTitle("Open Image");
				chooser.showOpenDialog(null);
				if (chooser.getSelectedFile() != null) {
					textFieldAvatar.setText(chooser.getSelectedFile()
							.getAbsolutePath());
				}

			}
		});
		btnM.setBounds(290, 197, 60, 23);
		panel.add(btnM);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (checkEmpty() == false) {
					JOptionPane.showMessageDialog(null,
							"Please Insert Full Information");
				} else {
					getInformationFromForm();
					insertToDatabase();
				}
			}
		});
		btnSave.setBounds(121, 233, 89, 23);
		panel.add(btnSave);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clear();
			}
		});
		btnCancel.setBounds(222, 233, 89, 23);
		panel.add(btnCancel);
	}

	private void insertToDatabase() {
		// TODO Auto-generated method stub
		TaiKhoanDAO tkDAO = new TaiKhoanDAO();
		if (tkDAO.save(tk)) {
			JOptionPane.showMessageDialog(null, "Insert Success");
		} else {
			JOptionPane.showMessageDialog(null, "Insert Fail");
		}
	}

	private void getInformationFromForm() {
		// TODO Auto-generated method stub

		tk = new TaiKhoan();
		tk.setUsername(textFieldUserName.getText());
		String password = passwordField.getPassword().toString();
		String hashpassword = HashPassword.MD5(password);
		tk.setPassword(hashpassword);
		tk.setHoten(textFieldHoTen.getText());
		tk.setEmail(textFieldEmail.getText());
		tk.setDienthoai(Integer.parseInt(textFieldDienThoai.getText()));
		tk.setCongty(textFieldCongTy.getText());
		tk.setDiachi(textFieldDiaChi.getText());
		String urlImage = textFieldAvatar.getText();

		byte[] res = ConvertImage.convertImageToByteArray(urlImage);
		tk.setAvatar(res);

	}

	private void clear() {
		// TODO Auto-generated method stub
		textFieldUserName.setText("");
		passwordField.setText("");
		textFieldHoTen.setText("");
		textFieldEmail.setText("");
		textFieldDienThoai.setText("");
		textFieldCongTy.setText("");
		textFieldDiaChi.setText("");
		textFieldAvatar.setText("");
	}

	private boolean checkEmpty() {
		if (textFieldUserName.getText().equals("")
				|| passwordField.getPassword().toString().equals("")
				|| textFieldHoTen.getText().equals("")
				|| textFieldEmail.getText().equals("")
				|| textFieldDienThoai.getText().equals("")
				|| textFieldCongTy.getText().equals("")
				|| textFieldDiaChi.getText().equals("")
				|| textFieldAvatar.getText().equals("")) {
			return false;
		} else {
			return true;
		}

	}
}
